<template>
  <div v-loading="isLoading > 0" class="page-container">
    <page-tools>
      <template #left>
        <!-- <span>总记录数: {{ total }} 条</span> -->
      </template>
      <template #right>
        <el-button type="primary" size="mini" @click="addCity">新增城市</el-button>
      </template>
    </page-tools>

    <el-card style="margin-top: 10px;">
      <el-table :data="cityList" border>
        <el-table-column label="序号" type="index" fixed="left" width="60" sortable />
        <el-table-column label="城市名" prop="city_name" width="100" />
        <el-table-column label="门店个数" width="110" sortable>
          <template #default="{ row }">
            <!-- <span>{{ row.shops.length }}</span> -->
            <router-link :to="'/shop/list?city_id=' + row.id" class="text-link">查看门店</router-link>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" width="100" sortable />
        <el-table-column label="是否上线" width="100">
          <template #default="{ row }">
            <el-switch
              :value="row.status === 1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="editCity(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <add-edit ref="AddEditDialog" @refresh="getCityList" />
  </div>
</template>

<script>
import { getCityList, updateOneCityById } from '@/api/city'
import { formatTime } from '@/filters/index'
import AddEdit from './components/AddEdit.vue'
export default {
  name: 'CityList',
  components: {
    AddEdit
  },
  filters: {
    formatTime
  },
  data() {
    return {
      isLoading: 0,
      cityList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getCityList()
  },
  mounted() {},
  methods: {
    addCity() {
      this.$refs.AddEditDialog.open()
    },
    editCity(row) {
      this.$refs.AddEditDialog.open(row)
    },
    changeStatus(row) {
      this.$confirm('确定要进行此次操作么？', '温馨提示').then(async() => {
        try {
          this.isLoading++
          await updateOneCityById({
            id: row.id,
            status: row.status === 1 ? -1 : 1
          })

          await this.getCityList()
        } finally {
          this.isLoading--
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    async getCityList() {
      try {
        this.isLoading++
        this.cityList = await getCityList()
      } finally {
        this.isLoading--
      }
    }
    // indexMethod(index) {
    //   return index + 1 + (this.page - 1) * this.size
    // },
    // handleSizeChange() {},
    // handleCurrentChange() {}
  }
}
</script>

<style lang="scss" scoped>
</style>
